home *** CD-ROM | disk | FTP | other *** search
- {$A+,B-,D-,E-,F-,G-,I-,L-,N-,O-,P-,Q-,R-,S+,T-,V+,X+,Z-}
- unit Engine;
- interface
-
- function Diff(WordA,WordB: ShortString): Integer;
-
- implementation
- uses
- SysUtils;
-
- procedure SortLetters(var WordA: ShortString);
- var
- i,j: Integer;
- tmp: Char;
- begin
- for i:=1 to Length(WordA) do
- begin
- for j:=Length(WordA) downto i+1 do
- begin
- if WordA[i] > WordA[j] then
- begin
- tmp := WordA[i];
- WordA[i] := WordA[j];
- WordA[j] := tmp
- end
- end
- end
- end {SortLetters};
-
- function Diff(WordA,WordB: ShortString): Integer;
- { returns 0, 1 or more characters difference }
- var
- i,j: Integer;
- OK: Boolean;
- begin
- WordA := UpperCase(WordA);
- WordB := UpperCase(WordB);
- if Length(WordA) <> Length(WordB) then
- begin
- if Length(WordB) > Length(WordA) then Result := Diff(WordB,WordA)
- else
- if Length(WordA) < (Length(WordB)+2) then { no more than one letter? }
- begin
- i := 1;
- repeat
- OK := False;
- for j:=1 to Length(WordB) do
- OK := OK OR (WordA[i] = WordB[i]);
- if not OK then Delete(WordA,i,1)
- else Inc(i)
- until (i > Length(WordA)) or (Length(WordA) = Length(WordB));
- if Length(WordA) = Length(WordB) then
- Result := 1 + Diff(WordA,WordB)
- else
- Result := 255 { no compare possible }
- end
- else
- Result := 255 { no sensible compare possible }
- end
- else
- begin
- Result := 0;
- for i:=1 to Length(WordA) do
- if WordA[i] <> WordB[i] then Result := Result + 1;
- if Result > 1 then { two letters replaced?? }
- begin
- SortLetters(WordA);
- SortLetters(WordB);
- Result := 1;
- for i:=1 to Length(WordA) do
- if WordA[i] <> WordB[i] then Result := Result + 1
- end
- end
- end {Diff};
- end.
-